//
//  LGTRWZPostViewCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/22.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZPostTypeCell: UITableViewCell {

    var titleLabel:UILabel?
    var myView:UIView?
    var buttonArray:Array<UIButton> = Array.init()
    private var _model:Dictionary<String, Any>?
    var typeSelectBlock:ClickPostNameModelBlock?
    var type_list:Array<PostNameModel>?
    var path:IndexPath?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
       self.titleLabel?.text = self.model["title"] as? String ?? ""
       // myView 高度计算
        var type_list:Array<PostNameModel>? = self.model["array"] as? Array<PostNameModel>
        if type_list == nil || type_list?.count == 0{
            return
        }
        self.type_list = type_list
        
        let count = CGFloat(4)
        let spaceX:CGFloat = SizeFitFloat(22)
        let spaceY:CGFloat = SizeFitFloat(12)
        let itemH:CGFloat = SizeFitFloat(42)
        let buttonH:CGFloat = SizeFitFloat(30)
        let itemW:CGFloat = (KScreenW -  (count - 1) * spaceX - SizeFitFloat(26)) / count
        let dataCount:Int = type_list!.count
        let row:Int = dataCount % 4 == 0 ?  (dataCount / 4) :   (dataCount / 4 + 1)
        let viewH:CGFloat = CGFloat(row) * itemH
        self.myView?.snp.updateConstraints({ (make) in
            make.height.equalTo(viewH)
        })
        
        //视图移除
        for i in 0..<self.buttonArray.count{
            let button:UIButton = self.buttonArray[i]
            button.removeFromSuperview()
        }
        
        for i in 0..<type_list!.count{
            let model:PostNameModel = type_list![i]
            if i+1 > self.buttonArray.count{
                let button:UIButton = LGTViewCreateUtil.share.createTextLGTButton(title: "", font: Font_Normal_15, color: TextColor, selectedColor: UIColor.white, corner: SizeFitFloat(15), backColor: ViewBackColor)
                self.buttonArray.append(button)
            }
            
            let button:UIButton = self.buttonArray[i]
            button.backgroundColor = ViewBackColor
            button.isSelected = false
            button.tag = i
            self.myView?.addSubview(button)
            button.snp.makeConstraints { (make) in
                make.width.equalTo(itemW)
                make.top.equalTo((spaceY + buttonH) * CGFloat(i/4))
                make.height.equalTo(buttonH)
                make.left.equalTo((spaceX + itemW) * CGFloat(i%4))
            }
            button.setTitle(model.name, for: UIControl.State.normal)
            button.setTitle(model.name, for: UIControl.State.selected)
            button.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
        }
        
        //默认状态选择
        let currentType:PostNameModel? = self.model["type"] as? PostNameModel
        if currentType != nil{
            for i in 0..<self.type_list!.count{
                let model = self.type_list![i]
                if model.value == currentType?.value{
                    self.clickAction(sender: self.buttonArray[i])
                }
            }
        }else{
            //默认选择
            if self.buttonArray.count > 0{
                self.clickAction(sender: self.buttonArray[0])
            }
        }
    }
    
    
    @objc func clickAction(sender:UIButton){
        let index:Int = sender.tag
        for i in 0..<self.buttonArray.count{
            let button:UIButton = self.buttonArray[i]
            button.backgroundColor = ViewBackColor
            button.isSelected = false
            if i == index{
                button.backgroundColor = YellowColor
                button.isSelected = true
            }
        }
        
        let data = self.type_list?[index]
        if self.typeSelectBlock != nil{
           self.typeSelectBlock!(data!, self.path!)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        let yellowView:UIImageView = UIImageView.init()
        yellowView.backgroundColor = YellowColor
        self.addSubview(yellowView)
        yellowView.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(4))
            make.height.equalTo(SizeFit(17))
            make.left.equalTo(SizeFit(13))
            make.top.equalTo(SizeFit(18))
        }

        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(18), text: "")
        self.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints({ (make) in
            make.centerY.equalTo(yellowView)
            make.height.equalTo(SizeFit(18))
            make.left.equalTo(SizeFit(26))
        })

        self.myView = UIView.init()
        self.addSubview(self.myView!)
        self.myView?.snp.makeConstraints({ (make) in
            make.left.equalTo(SizeFit(13))
            make.right.equalTo(-SizeFit(13))
            make.top.equalTo(SizeFit(53))
            make.bottom.equalTo(SizeFit(-6))
            make.height.equalTo(SizeFit(0))
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
